<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="批次ID" prop="batchId">
        <el-input
          v-model="queryParams.batchId"
          placeholder="请输入批次ID，关联物料批次表 mes_material_batches"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料ID" prop="materialId">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入物料ID，关联物料主表 mes_materials"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入交易数量，本次物料交易的数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="unitOfMeasure">
        <el-input
          v-model="queryParams.unitOfMeasure"
          placeholder="请输入计量单位，与交易数量配套，如个、千克"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易时间" prop="transactionTime">
        <el-date-picker clearable
          v-model="queryParams.transactionTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择交易时间，默认当前时间，记录交易发生时刻">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="关联工单ID" prop="relatedWorkOrderId">
        <el-input
          v-model="queryParams.relatedWorkOrderId"
          placeholder="请输入关联工单ID，可选，关联本次交易相关的工单"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联操作记录ID" prop="relatedOperationRecordId">
        <el-input
          v-model="queryParams.relatedOperationRecordId"
          placeholder="请输入关联操作记录ID，可选，关联操作记录"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人ID" prop="responsibleUserId">
        <el-input
          v-model="queryParams.responsibleUserId"
          placeholder="请输入责任人ID，可选，记录负责本次交易的用户"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间，记录本条交易记录的创建时刻">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['material-service:transactions:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material-service:transactions:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material-service:transactions:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['material-service:transactions:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transactionsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="交易记录ID，自增唯一标识每条物料交易" align="center" prop="transactionId" />
      <el-table-column label="批次ID，关联物料批次表 mes_material_batches" align="center" prop="batchId" />
      <el-table-column label="物料ID，关联物料主表 mes_materials" align="center" prop="materialId" />
      <el-table-column label="交易类型，如入库、出库、领用等业务类型" align="center" prop="transactionType" />
      <el-table-column label="交易数量，本次物料交易的数量" align="center" prop="quantity" />
      <el-table-column label="计量单位，与交易数量配套，如个、千克" align="center" prop="unitOfMeasure" />
      <el-table-column label="交易时间，默认当前时间，记录交易发生时刻" align="center" prop="transactionTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.transactionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关联工单ID，可选，关联本次交易相关的工单" align="center" prop="relatedWorkOrderId" />
      <el-table-column label="关联操作记录ID，可选，关联操作记录" align="center" prop="relatedOperationRecordId" />
      <el-table-column label="责任人ID，可选，记录负责本次交易的用户" align="center" prop="responsibleUserId" />
      <el-table-column label="备注信息，可存交易相关说明、异常备注等" align="center" prop="notes" />
      <el-table-column label="创建时间，记录本条交易记录的创建时刻" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['material-service:transactions:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['material-service:transactions:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改MES 物料交易，记录物料入库、出库等交易全流程信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="transactionsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="批次ID" prop="batchId">
          <el-input v-model="form.batchId" placeholder="请输入批次ID，关联物料批次表 mes_material_batches" />
        </el-form-item>
        <el-form-item label="物料ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料ID，关联物料主表 mes_materials" />
        </el-form-item>
        <el-form-item label="交易数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入交易数量，本次物料交易的数量" />
        </el-form-item>
        <el-form-item label="计量单位" prop="unitOfMeasure">
          <el-input v-model="form.unitOfMeasure" placeholder="请输入计量单位，与交易数量配套，如个、千克" />
        </el-form-item>
        <el-form-item label="交易时间" prop="transactionTime">
          <el-date-picker clearable
            v-model="form.transactionTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择交易时间，默认当前时间，记录交易发生时刻">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交易类型" prop="transactionType">
          <el-input v-model="form.transactionType" placeholder="请输入交易类型，入库，出库，领用" />
        </el-form-item>
        <el-form-item label="关联工单ID" prop="relatedWorkOrderId">
          <el-input v-model="form.relatedWorkOrderId" placeholder="请输入关联工单ID，可选，关联本次交易相关的工单" />
        </el-form-item>
        <el-form-item label="关联操作记录ID" prop="relatedOperationRecordId">
          <el-input v-model="form.relatedOperationRecordId" placeholder="请输入关联操作记录ID，可选，关联操作记录" />
        </el-form-item>
        <el-form-item label="责任人ID" prop="responsibleUserId">
          <el-input v-model="form.responsibleUserId" placeholder="请输入责任人ID，可选，记录负责本次交易的用户" />
        </el-form-item>
        <el-form-item label="备注信息" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间，记录本条交易记录的创建时刻">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Transactions">
import { listTransactions, getTransactions, delTransactions, addTransactions, updateTransactions } from "@/api/material-service/transactions"

const { proxy } = getCurrentInstance()

const transactionsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    batchId: null,
    materialId: null,
    transactionType: null,
    quantity: null,
    unitOfMeasure: null,
    transactionTime: null,
    relatedWorkOrderId: null,
    relatedOperationRecordId: null,
    responsibleUserId: null,
    notes: null,
    createdAt: null
  },
  rules: {
    batchId: [
      { required: true, message: "批次ID，关联物料批次表 mes_material_batches不能为空", trigger: "blur" }
    ],
    materialId: [
      { required: true, message: "物料ID，关联物料主表 mes_materials不能为空", trigger: "blur" }
    ],
    transactionType: [
      { required: true, message: "交易类型，如入库、出库、领用等业务类型不能为空", trigger: "change" }
    ],
    quantity: [
      { required: true, message: "交易数量，本次物料交易的数量不能为空", trigger: "blur" }
    ],
    unitOfMeasure: [
      { required: true, message: "计量单位，与交易数量配套，如个、千克不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询MES 物料交易，记录物料入库、出库等交易全流程信息列表 */
function getList() {
  loading.value = true
  listTransactions(queryParams.value).then(response => {
    transactionsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    transactionId: null,
    batchId: null,
    materialId: null,
    transactionType: null,
    quantity: null,
    unitOfMeasure: null,
    transactionTime: null,
    relatedWorkOrderId: null,
    relatedOperationRecordId: null,
    responsibleUserId: null,
    notes: null,
    createdAt: null
  }
  proxy.resetForm("transactionsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.transactionId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加MES 物料交易，记录物料入库、出库等交易全流程信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _transactionId = row.transactionId || ids.value
  getTransactions(_transactionId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改MES 物料交易，记录物料入库、出库等交易全流程信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["transactionsRef"].validate(valid => {
    if (valid) {
      if (form.value.transactionId != null) {
        updateTransactions(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addTransactions(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _transactionIds = row.transactionId || ids.value
  proxy.$modal.confirm('是否确认删除MES 物料交易，记录物料入库、出库等交易全流程信息编号为"' + _transactionIds + '"的数据项？').then(function() {
    return delTransactions(_transactionIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('material-service/transactions/export', {
    ...queryParams.value
  }, `transactions_${new Date().getTime()}.xlsx`)
}

getList()
</script>
